<script setup>
import { onMounted, reactive, ref } from "vue";
import { accountListApi, userEditApi, menuSelectApi } from "../../../api/user";
import dayjs from "dayjs"; // 引入dayjs
const page = reactive({
  pageNum: 1,
  pageSize: 10,
});
const handleSizeChange = (val) => {
  page.pageSize = val;
  accountList();
};
const handleCurrentChange = (val) => {
  page.pageNum = val;
  accountList();
};
// 获取列表
const tableData = reactive([]);
const total = ref(0);
// 权限组列表
const permissionData = ref([]);
// 获取账号列表
const accountList = async () => {
  const res = await accountListApi(page);
  total.value = res.data.total;
  Object.assign(tableData, res.data.list);
  // 时间格式化
  tableData.forEach((item) => {
    item.create_time = dayjs(item.create_time).format("YYYY-MM-DD HH:mm:ss");
  });
};
// 权限下拉列表
const permissionList = async () => {
  const res = await menuSelectApi();
  permissionData.value = res.data;
};
// 获取权限组名称
const getPermissionName = (id) => {
  const item = permissionData.value.find((item) => item.id === id);
  return item ? item.name : "";
};

// 弹窗表单数据
const form = ref({});
const rules = {
  name: [
    { required: true, message: "请输入权限组名称", trigger: "blur" },
    { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" },
  ],
  permissions_id: [
    { required: true, message: "请选择权限组", trigger: "change" },
  ],
};
// 弹窗状态
const dialogVisible = ref(false);
// 点击编辑
const handleEdit = (row) => {
  // 数据回填
  form.value = { ...row };
  dialogVisible.value = true;
};
// 点击关闭弹窗
const handleClose = () => {
  dialogVisible.value = false;
  form.value = {};
};
const formRef = ref(null);
// 提交表单
const confirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      console.log("校验成功");
      // 提交表单
      let { name, permissions_id } = form.value;
      const res = await userEditApi({ name, permissions_id });
      if (res.code === 10000) {
        ElMessage.success("修改成功");
        handleClose();
        accountList();
      } else {
        ElMessage.error("修改失败");
      }
    } else {
      console.log("校验失败");
      return false;
    }
  });
};
onMounted(() => {
  accountList();
  permissionList();
});
</script>

<template>
  <panelHead />
  <el-card>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号">
        <template #default="scope">
          {{ (page.pageNum - 1) * page.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="ID" prop="id" />
      <el-table-column label="昵称" prop="name" />
      <el-table-column label="所属组别" prop="permissions_id">
        <template #default="{ row }">
          {{ getPermissionName(row.permissions_id) }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="mobile" />
      <el-table-column label="状态" prop="active">
        <template #default="{ row }">
          <el-tag :type="row.active ? 'primary' : 'danger'">
            {{ row.active ? "正常" : "失效" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="create_time">
        <template #default="{ row }">
          <el-icon><Clock /></el-icon>
          {{ row.create_time }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="primary" @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-info">
      <el-pagination
        :v-model:currentPage="page.pageNum"
        :page-size="page.pageSize"
        layout="total, prev,
  pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
  <el-dialog
    v-model="dialogVisible"
    title="编辑用户"
    width="500"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :rules="rules"
      :model="form"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="手机号" prop="form.mobile">
        <el-input
          placeholder="请输入手机号"
          type="number"
          disabled
          v-model="form.mobile"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="name" required>
        <el-input placeholder="请输入昵称" v-model="form.name" />
      </el-form-item>
      <el-form-item label="菜单权限" prop="permissions_id" required>
        <el-select v-model="form.permissions_id" placeholder="请选择">
          <el-option
            v-for="item in permissionData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" size="small" @click="confirm(formRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
